﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="hrSalaryFormula.aspx.cs" Inherits="ZX.WebApp.EHR.hrSalaryFormula" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
      body
      { 
          font-family: 宋体, Arial, Helvetica, sans-serif;
          font-size: 12px;
      } 
      input
      {
          width:65px;
          height:30px;
      }
      textarea
      {
          width:400px;
          overflow:hidden;
      }
      select
      {
          width:100%;
      }
      .td_text
      {
         font-size:smaller; 
         text-align:left;
      }
    </style>
    <script src="../Scripts/jquery-1.6.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/json/json-min.js"></script>
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/connection/connection-min.js"></script>
    <script src="../Scripts/Public.js" type="text/javascript"></script>
    <script type="text/javascript">
        var expression = [];//表达式
        $(document).ready(function () {
            var flag = getPar1("flag");
            if (flag == "2") { //选择薪资函数参数时,将薪资项和函数项部门隐藏
                $("#lableFunc").hide();
                $("#func").hide();
            }
            if (flag == "1") { //薪资函数时，将函数部分替换为函数参数
                $("#lableFunc").html("函数参数:");
                //加载父页参数
                $("#func").find("option").remove();
                $("#func").append("<option value=''></option>");
                for (var i = 0; i < parent._DataTable.Rows.length; i++) {
                    var row = parent._DataTable.Rows[i];
                    $("#func").append("<option value='" + row.Parameter + "' title='" + row.Parameter + "'>" + row.Parameter + "</option>");
                }
            }
            if (flag != "2" && flag != "1") {
                //加载薪资函数
                var myurl = "../Extend/AjaxData/jsonlist.aspx?entity=salSalaryFunction";
                $.get(myurl, function (d, s) {
                    var list = eval("(" + d + ")");
                    if (list.total > 0) {
                        $("#func").find("option").remove();
                        $("#func").append("<option value=''></option>");
                        for (var o in list.rows) {
                            var v = list.rows[o].Functions;
                            var t = list.rows[o].Name;
                            if (v != "undefined" && t != "undefined") {
                                $("#func").append("<option value='" + v + "' title='" + t + "'>" + t + "</option>");
                            }
                        }
                    }
                });
            }
            $("#txtResult").val(parent.FormulaData);
            $("#txtResult").focus();
            parent.$("#okay").mousedown(function () {
                parent.FormulaData = $("#txtResult").val();
            });
            //加载数据表
            var url = "../Extend/AjaxData/jsonlist.aspx?entity=salSalaryRelationTable";
            var tempv = null;
            $.get(url, function (d, s) {
                var list = eval("(" + d + ")");
                if (list.total > 0) {
                    $("#datatable").find("option").remove();
                    $("#datatable").append("<option value=''></option>");
                    for (var o in list.rows) {
                        var v = list.rows[o].ID;
                        var t = list.rows[o].Name;
                        if (v != "undefined" && t != "undefined") {
                            $("#datatable").append("<option value='" + v + "' title='" + t + "'>" + t + "</option>");
                            tempv = v;
                        }
                    }
                }
                if (tempv) {
                    $("#datatable").trigger("change");
                }
            });

            //加载薪资项
            url = "../Extend/AjaxData/jsonlist.aspx?entity=salSalaryItem&condition=SalarySolutionID='00000000-0000-0000-0000-000000000000'&r=" + Math.random();
            $.get(url, function (d, s) {
                var list = eval("(" + d + ")");
                if (list.total > 0) {
                    $("#salaryItem").find("option").remove();
                    $("#salaryItem").append("<option value=''></option>");
                    for (var o in list.rows) {
                        var v = list.rows[o].ItemName;
                        var t = list.rows[o].ItemNotes;
                        if (v != "undefined" && t != "undefined") {
                            $("#salaryItem").append("<option value='" + v + "' title='" + t + "'>" + t + "</option>");
                        }
                    }
                }
            });

            //选中薪资项
            $("#salaryItem").change(function () {
                var tempText = "";
                var obj = $("#salaryItem")[0];
                for (var i = 0; i < obj.length; i++) {
                    if (obj[i].value == $("#salaryItem").val()) {
                        tempText = obj[i].text;
                    }
                }
                var v = $("#salaryItem").val();
                if (v == "") return;
                $("#txtResult").insertAtCaret("{" + tempText + "}");
            });

            //选中函数
            $("#func").change(function () {
                var tempText = "";
                var obj = $("#func")[0];
                for (var i = 0; i < obj.length; i++) {
                    if (obj[i].value == $("#func").val()) {
                        tempText = obj[i].text;
                    }
                }
                var v = $("#func").val();
                if (v == "") return;
                $("#txtResult").insertAtCaret("<" + tempText + ">");
            });

            //选中字段
            $("#field").change(function () {
                var tempText = "";
                var obj = $("#datatable")[0];
                for (var i = 0; i < obj.length; i++) {
                    if (obj[i].value == $("#datatable").val()) {
                        tempText = obj[i].text;
                    }
                }
                var v = $("#field").val();
                if (v == "") return;
                var tempfield = "";
                var fieldobj = $("#field")[0];
                for (var i = 0; i < fieldobj.length; i++) {
                    if (fieldobj[i].value == $("#field").val()) {
                        tempfield = fieldobj[i].text;
                    }
                }
                if (tempText != "" && tempfield != "") {
                    v = "[" + tempText + "." + tempfield + "]";
                }
                $("#txtResult").insertAtCaret(v);
            });

            //数据表change事件,加载数据表字段
            $("#datatable").change(function () {
                var tableId = $("#datatable").val();
                if (tableId == "") return;
                var myurl = "../Extend/AjaxData/jsonlist.aspx?entity=salSalaryRelationFields&condition=salRelationTableID='" + tableId + "'";
                $.get(myurl, function (d, s) {
                    var list = eval("(" + d + ")");
                    if (list.total > 0) {
                        $("#field").find('option').remove();
                        $("#field").append("<option value=''></option>");
                        for (var o in list.rows) {
                            var v = list.rows[o].FieldName;
                            var t = list.rows[o].FieldDes;
                            if (v != "undefined" && t != "undefined") {
                                $("#field").append("<option value='" + v + "' title='" + t + "'>" + t + "</option>");
                            }
                        }
                    }
                });
            });
            //按钮单击事件
            $("input").each(function () {
                $(this).click(function () {
                    var btnValue = $(this).val();
                    if (btnValue == "") return;
                    var rs = $("#txtResult").val();
                    if (btnValue == "清除") {
                        $("#txtResult").val("");
                        return;
                    }
                    else if (btnValue == "退格") {
                        rs = rs.substr(0, rs.length - 1);
                        $("#txtResult").val(rs);
                        return;
                    }
                    else if (GetObjectType(btnValue) == "op") {
                        var last = rs[rs.length - 1];
                        if (GetObjectType(last) == "op") {
                            rs = rs.substr(0, rs.length - 1);
                        }
                    }
                    $("#txtResult").insertAtCaret(btnValue);
                });
            });
        });

        //判断类型是数据（包括数字，字段），操作符（+-*/），函数，正括号，反括号,清除，退格
        function GetObjectType(o) {
            if (!isNaN(o)) {
                return "num";
            }
            if (o == "+" || o == "-" || o == "*" || o == "/") {
                return "op";
            }
            if (o == "(" || o == ")" || o == "清除" || o == "退格") {
                return o;
            }
        }

(function ($) {
    $.fn.extend({
        insertAtCaret: function (myValue) {
            var $t = $(this)[0];
            if (document.selection) {
                this.focus();
                sel = document.selection.createRange();
                sel.text = myValue;
                this.focus();
            }
            else
                if ($t.selectionStart || $t.selectionStart == '0') {
                    var startPos = $t.selectionStart;
                    var endPos = $t.selectionEnd;
                    var scrollTop = $t.scrollTop;
                    $t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length);
                    this.focus();
                    $t.selectionStart = startPos + myValue.length;
                    $t.selectionEnd = startPos + myValue.length;
                    $t.scrollTop = scrollTop;
                }
                else {
                    this.value += myValue;
                    this.focus();
                }
        }
    })
})(jQuery);

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <table>
        <tr>
            <td colspan="6">
            <textarea  cols="20" id="txtResult" rows="3"></textarea>
            </td>
        </tr>
        <tr>
            <td class="td_text">选数据表:</td>
            <td colspan="2"><select id="datatable" ></select></td>
            <td class="td_text">&nbsp;&nbsp;选字段:</td>
            <td colspan="2"><select id="field"></select></td>
        </tr>
        <tr>
            <td><input type="button" id="btnPingFang" value="平方"/></td>
            <td><input type="button" id="btn7" value="7"/></td>
            <td><input type="button" id="btn8" value="8"/></td>
            <td><input type="button" id="btn9" value="9"/></td>
            <td><input type="button" id="btnClear" value="清除"/></td>
            <td><input type="button" id="btnBack" value="退格"/></td>
        </tr>
        <tr>
            <td><input type="button" id="btnKaiFang" value="开方"/></td>
            <td><input type="button" id="btn4" value="4"/></td>
            <td><input type="button" id="btn5" value="5"/></td>
            <td><input type="button" id="btn6" value="6"/></td>
            <td><input type="button" id="btnKuo1" value="（"/></td>
            <td><input type="button" id="btnKuo2" value="）"/></td>
        </tr>
        <tr>
            <td><input type="button" id="btnDaoShu" value="1/x"/></td>
            <td><input type="button" id="btn1" value="1"/></td>
            <td><input type="button" id="btn2" value="2"/></td>
            <td><input type="button" id="btn3" value="3"/></td>
            <td><input type="button" id="btnJia" value="+"/></td>
            <td><input type="button" id="btnJian" value="-"/></td>
        </tr>
        <tr>
            <td><input type="button" id="btn" value="" /></td>
            <td><input type="button" id="btn0" value="0"/></td>
            <td><input type="button" id="btnDot" value="."/></td>
            <td><input type="button" id="btnPercent" value="%"/></td>
            <td><input type="button" id="btnCheng" value="*"/></td>
            <td><input type="button" id="btnChu" value="/"/></td>
        </tr>
        <tr>
          <td id="lableFunc" class="td_text">可选函数：</td>
          <td colspan="2">
            <select id="func"></select>
          </td>
          <td id="lableSalaryItem" class="td_text">&nbsp;&nbsp;薪资项目:</td>
          <td colspan="2">
            <select id="salaryItem"></select>
          </td>
        </tr>
        </table>
    </div>
    </form>
</body>
</html>
